package com.yjzx.util.common.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.yjzx.util.common.util.excel.alibaba.ColumnHandler;
import com.yjzx.util.common.util.excel.alibaba.ExcelListener;
import com.yjzx.util.common.util.map.NodeMap;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yjzx
 * @date 2024/8/19
 * @description main
 */
@Slf4j
public class Main {
    private static final String CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static final Random RANDOM = new Random();

    public static BufferedImage generateCaptchaImage(int width, int height, int length) {
        // 创建空白图像
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();

        // 设置背景颜色
        g2d.setColor(new Color(240, 240, 240));
        g2d.fillRect(0, 0, width, height);

        // 绘制验证码字符
        StringBuilder captchaText = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = RANDOM.nextInt(CHARACTERS.length());
            char randomChar = CHARACTERS.charAt(index);
            captchaText.append(randomChar);

            // 随机字体大小
            int fontSize = 24 + RANDOM.nextInt(6);
            Font font = new Font("Arial", Font.BOLD, fontSize);
            g2d.setFont(font);

            // 随机颜色
            g2d.setColor(new Color(RANDOM.nextInt(256), RANDOM.nextInt(256), RANDOM.nextInt(256)));
            g2d.drawString(String.valueOf(randomChar), (i * width / length) + 5, height - 10);
        }

        // 添加干扰线
        for (int i = 0; i < 10; i++) {
            g2d.setColor(new Color(RANDOM.nextInt(256), RANDOM.nextInt(256), RANDOM.nextInt(256)));
            int x1 = RANDOM.nextInt(width);
            int y1 = RANDOM.nextInt(height);
            int x2 = RANDOM.nextInt(width);
            int y2 = RANDOM.nextInt(height);
            g2d.drawLine(x1, y1, x2, y2);
        }

        // 关闭图形上下文
        g2d.dispose();

        // 返回带有验证码的图像
        return image;
    }
    public static void main(String[] args) throws IOException {
        //exec("C:\\Users\\admin\\Desktop\\新杭州离线故障情况（所有故障离线点位）.xlsx", "C:\\Users\\admin\\Desktop\\新杭州离线故障情况（范围及数量）.xlsx");
        BufferedImage captchaImage = generateCaptchaImage(120, 40, 6);
        ImageIO.write(captchaImage, "png", new java.io.File("asdf.png"));
    }

}
